function affichage_popup(page,width,height){
window.open ('./indexpopup.php?page='+page,'popupajout', config='height='+height+', width='+width+', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no')
}

function reload_page(page){
window.close()
window.opener.document.location.href="./index.php?page="+page;
}

function get_xhr(){
	var xhr;
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return xhr;
}

function refresh_serv(server,element){
	var xhr;
    xhr = get_xhr();
    xhr.onreadystatechange = function() { 
        if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
        	if(element == undefined){
        		location.reload();
        	}else{
        		element.src = "./img/refresh-mini.png";
        	}
        }else if(element != undefined){
        	element.src = "./img/notrefresh-mini.png";
        }
    }

//    on appelle le ping
    xhr.open("GET", "./index.php?update=ok&server="+server, true);
    xhr.send(null);
}

//pour la page d'acceuil
function refresh_last_type(typed){
	location.href = "index.php?sqltype="+typed;
}

first = 0;
function vide(element){
	var box=element;
	if ((box.value=="search")&&(first==0)){
		box.value="";
		first =1;
	}
}


function ajax_multiping(arrayaddress){
	for (var i = 0; i < arrayaddress.length; i++){
		ajax_ping(arrayaddress[i]);
		setInterval("ajax_ping('"+arrayaddress[i]+"');",5000);
	  }	
}

function ajax_ping(address){
	var xhr;
    xhr = get_xhr();
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_ajax(xhr,address); };
        
    //on appelle le ping
    xhr.open("GET", "./index.php?ping="+address, true);
    xhr.send(null);
}

function alert_ajax(xhr,address){
	  if (xhr.readyState==4){
		  var castr;
		  var ping;
		  castr = document.getElementById(address);
		  ping = xhr.responseText;
		  //alert(ping);
		  if (ping == "-1"){
			  castr.innerHTML = "Down";
		  }
		  else{
			  castr.innerHTML = ping;
		  }

	  }
}

function info_serv_up(array){
	for (var i = 0; i < array.length; i++){
		ajax_info_serv_up(array[i]);
		setInterval("ajax_info_serv_up('"+array[i]+"');",20000);
	}	
}
function ajax_info_serv_up(address){
	var xhr;
    xhr = get_xhr();
    xhr.onreadystatechange = function() { color_serv_up(address,xhr); };
        
    //on appelle le ping
    xhr.open("GET", "./index.php?ping="+address, true);
    xhr.send(null);
}

function color_serv_up(serve,xhr){
	if (xhr.readyState==4){
		ping = xhr.responseText;
		if(ping == "-1"){
			var servtag = document.getElementsByName(serve);
			for(var i=0;i<servtag.length;i++){
				servtag.item(i).style.backgroundColor = "#ef8e74";
			}
		}else{
			var servtag = document.getElementsByName(serve);
			for(var i=0;i<servtag.length;i++){
				servtag.item(i).style.backgroundColor = "#a5ef74";
			}
		}
		
	}
}

function ajax_add_favorite(id){
	var xhr;
    xhr = get_xhr();
    xhr.open("GET", "./index.php?page=favorite&action=add_favorite&id="+id,true);
    xhr.send(null);
}
function ajax_rm_favorite(id){
	var xhr;
    xhr = get_xhr();
    xhr.open("GET", "./index.php?page=favorite&action=remove_favorite_element_ajax&id="+id,true);
    xhr.send(null);
}

function ajax_refresh_last_type(type,size){
	var xhr;
    xhr = get_xhr();
    xhr.open("GET", "./index.php?action=ajax&sqltype="+type+"&size="+size,true);
    xhr.send(null);
    xhr.onreadystatechange = function() { ajax_set_last_type(xhr);};
}

function ajax_change_status_menu(type){
    	var element = document.getElementById("status_menu");
    	var menu = document.getElementById("search_menu");
    	var tmp;
    	var tmp2;
    	
    	if(menu.style.position==""){
    		tmp2 = type;
    	}else{
    		tmp2 = menu.style.position;
    	}
    	//alert(tmp2);
    	if(tmp2=="fixed"){
    		tmp = "relative";
    	}else{
    		tmp = "fixed";
    	}
    	
    	element.src = "./img/"+tmp+".png";
    	element.alt = tmp;
    	
    	menu.style.position = tmp;

    	var xhr;
	    xhr = get_xhr();
	    xhr.open("GET", "./index.php?action=ajax&menu_status="+tmp,true);
	    xhr.send(null);
   
}

function ajax_set_last_type(xhr){
	//alert(xhr.readyState);
	var castr = document.getElementById("div_last_film");
	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)){
		var reponse;
		reponse = xhr.responseText;
		castr.innerHTML = reponse;
	}
}
